package br.com.spam.pipeline.iu;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import br.com.spam.pipeline.core.Configuration;

public class IU_ExOptions extends JPanel
{
	private static final long serialVersionUID = 5322712093882968395L;

	private IU_Datashow parent;

	private JButton play = new JButton(new ImageIcon("resources/play.png"));
	private JButton next = new JButton(new ImageIcon("resources/next.png"));
	private JButton pause = new JButton(new ImageIcon("resources/pause.png"));
	private JComboBox file = new JComboBox();
	private JCheckBox bypass = new JCheckBox();
	private JCheckBox preditorDesvio = new JCheckBox();

	public IU_ExOptions(IU_Datashow parent)
	{
		final JPanel topButtons = new JPanel();

		this.parent = parent;
		this.parent.onPipelineChanged(new Observer()
		{
			@Override
			public void update(Observable o, Object arg)
			{
				topButtons.setEnabled(arg != null);
			}
		});

		this.setBackground(Color.WHITE);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		topButtons.setLayout(new GridLayout(0, 3));

		topButtons.add(this.play);
		topButtons.setEnabled(false);
		this.play.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				IU_ExOptions.this.parent.pipeline().play();
			}
		});

		topButtons.add(this.next);
		this.next.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				IU_ExOptions.this.parent.pipeline().next();
			}
		});

		topButtons.add(this.pause);
		this.pause.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				IU_ExOptions.this.parent.pipeline().pause();
			}
		});
		this.add(topButtons);

		this.loadAvailableSourceCode();
		this.add(this.file);
		this.file.addItemListener(new ItemListener()
		{

			@Override
			public void itemStateChanged(ItemEvent e)
			{
				if (e.getStateChange() == ItemEvent.SELECTED)
				{
					IU_ExOptions.this.parent.load("Resources" + File.separator
							+ (String) e.getItem());
				}
			}
		});

		this.bypass.setText("Habilitar Bypass");
		this.add(this.bypass);
		this.bypass.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				Configuration.bypassEnabled = IU_ExOptions.this.bypass.isSelected();
			}
		});

		this.preditorDesvio.setText("Habilitar preditor de desvio");
		this.add(this.preditorDesvio);
		this.preditorDesvio.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				Configuration.preditorDesvio = IU_ExOptions.this.preditorDesvio.isSelected();
			}
		});
	}
	private void loadAvailableSourceCode()
	{
		File resourcesDirectory = new File("resources");

		this.file.setModel(new DefaultComboBoxModel(resourcesDirectory.list(new FilenameFilter()
		{
			public boolean accept(File dir, String name)
			{
				return name.toLowerCase().endsWith(".mips");
			}
		})));

		this.file.setSelectedItem(null);
	}
}
